package njust.edu.service.impl;

import njust.edu.dao.DishDao;
import njust.edu.dao.DishTypeDao;
import njust.edu.dao.UserDao;
import njust.edu.dao.impl.DishDaoImpl;
import njust.edu.dao.impl.DishTypeDaoImpl;
import njust.edu.dao.impl.UserDaoImpl;
import njust.edu.entity.Dish;
import njust.edu.entity.DishType;
import njust.edu.service.DishService;
import njust.edu.web.DishTypeSale;
import njust.edu.web.Page;

import java.util.ArrayList;
import java.util.List;

public class DishServiceImpl implements DishService {

    DishDao dishdao = new DishDaoImpl();
    DishTypeDao dishTypeDao = new DishTypeDaoImpl();
//    MessageDao messageDao = new MessageDaoImpl();
    UserDao userDao = new UserDaoImpl();

    @Override
    public Page<Dish> getPage(int n, String condition, String likeStr) {
        Page<Dish> page = dishdao.getPage(n, condition, likeStr);

        if (page.getList() != null) {
            for (Dish d : page.getList()) {
                d.setDishType(dishTypeDao.getById(d.getType_id()));
            }
        }
        return page;
    }

    @Override
    public List<DishType> getDishList() {
        return dishTypeDao.getList();
    }

    @Override
    public void addBook(String id,String name, Integer type_id, Float price,
                        String picture,int sale, Integer stock, String description) {
        Dish dish = new Dish(id,name, type_id, price, picture, sale, stock, description);
        dishdao.add(dish);
    }

    @Override
    public Dish getDishById(String id) {
        Dish dish = dishdao.findById(id);
//        dish.setType_id(dishTypeDao.getById(dish.getType_id()).getId());
        return dish;
    }

    @Override
    public void editBook(String id, String name, Integer type_id, Float price,
                         String picture,int sale, Integer stock, String description) {
        Dish dish = new Dish(id, name, type_id, price, picture, sale, stock, description);
        dishdao.update(dish);
    }

    public void deleteBook(String id) {
        dishdao.delete(id);
    }

    @Override
    public Integer changeStock(String id, Integer stock) {
        Dish dish = dishdao.findById(id);

        dish.setStock(stock);
        dishdao.update(dish);
        Integer stockNow = dishdao.findById(id).getStock();
        return stockNow;
    }

    @Override
    public Boolean checkStock(String id, Integer stockNum) {
        Dish dish = dishdao.findById(id);
        int DStock = dish.getStock();

        if (DStock - stockNum > 0) {
            return true;
        }
        return false;

    }

    @Override
    public List<DishTypeSale> getDishTypeSales() {
        List<DishTypeSale> dishTypeSales = new ArrayList<DishTypeSale>();

        for (DishType d : dishTypeDao.getList()) {
            DishTypeSale dishTypeSale = new DishTypeSale();
            dishTypeSale.setDishType(d);
            dishTypeSale.setSale(dishTypeDao.getDishTypeSale(d.getId()));
            dishTypeSales.add(dishTypeSale);
        }
        return dishTypeSales;
    }

    @Override
    public List<Dish> getAllDishList() {
        return dishdao.getList();
    }

    @Override
    public List<Dish> getAllDishList(String keyword) {
        return dishdao.getListByName(keyword);
    }

}
